/**
  * This module translates a string containing subscripts and superscripts
  * in the form
  *     string_subscript^superscript
  * into a string containing properly encoded subscripts and superscripts
  * in Unicode.
  *
  * @author Aaron A. Lovato
  */
package gui;

import java.util.TreeMap;

public class UnicodeTranslator
{
    private TreeMap<Character, String> subscripts;
    private TreeMap<Character, String> superscripts;
    
    public UnicodeTranslator()
    {
        //Build the maps of subscripts and superscripts
        subscripts = new TreeMap<Character, String>();
        superscripts = new TreeMap<Character, String>();
        subscripts.put('0', "\u2080");
        subscripts.put('1', "\u2081");
        subscripts.put('2', "\u2082");
        subscripts.put('3', "\u2083");
        subscripts.put('4', "\u2084");
        subscripts.put('5', "\u2085");
        subscripts.put('6', "\u2086");
        subscripts.put('7', "\u2087");
        subscripts.put('8', "\u2088");
        subscripts.put('9', "\u2089");

        superscripts.put('0', "\u2070");
        superscripts.put('1', "\u00b9");
        superscripts.put('2', "\u00b2");
        superscripts.put('3', "\u00b3");
        superscripts.put('4', "\u2074");
        superscripts.put('5', "\u2075");
        superscripts.put('6', "\u2076");
        superscripts.put('7', "\u2077");
        superscripts.put('8', "\u2078");
        superscripts.put('9', "\u2079");
        superscripts.put('+', "\u207a");
        superscripts.put('-', "\u207b");

    }
    public String translate(String operand)
    {
        //Resulting string
        StringBuffer result = new StringBuffer();

        //Variables to control the translation process
        boolean inSubSection = false;
        boolean inSuperSection = false;
        char current;
        String translated;

        //Set the double harpoons symbol
        operand = operand.replaceAll("<=>", "\u21cc");
        //Translate each character
        for(int index = 0; index < operand.length(); index++)
        {
            current = operand.charAt(index);
            if(current == '_')
            {
                inSubSection = true;
                inSuperSection = false;
                continue;
            }
            else if(current == '^')
            {
                inSubSection = false;
                inSuperSection = true;
                continue;
            }
            else if(current == ' ')
            {
                inSubSection = false;
                inSuperSection = false;
                result.append(current);
                continue;
            }
            else if (inSubSection)
            {
                translated = subscripts.get(current);
                if(translated != null)
                {
                    result.append(translated);
                }
                else
                {
                    result.append(current);
                }
            }
            else if (inSuperSection)
            {
                translated = superscripts.get(current);
                if(translated != null)
                {
                    result.append(translated);
                }
                else
                {
                    result.append(current);
                }
            }
            else
            {
                result.append(current);
            }
        }

        //Return the resulting translated string
        return result.toString();
    }
}